﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CXMLStudioIDE;


namespace CXMLStudioIDE.DataAccessLayer
{
    public class CXMLStudioDAL : BaseDAL
    {
        public DataSet GetVersionedBusinessEntityAndCode(int i_EntityTypeVersionID)
        {
            DataSet DS = new DataSet();
            System.Data.SqlClient.SqlConnection con = GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("dbo.getVersionedBusinessEntityAndCode", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("int_EntityTypeVersionID", i_EntityTypeVersionID);
            con.Open();
            da.Fill(DS);
            da.Dispose();
            con.Close();
            return DS;

        }

        public DataSet GetBusinessEntitiesForCompilation(int i_EntityID)
        {
            DataSet DS = new DataSet();
            System.Data.SqlClient.SqlConnection con = GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("dbo.getVersionedBusinessEntityForCompilation", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("int_EntityVersionID", i_EntityID);
            da.SelectCommand.CommandTimeout = 120;
            con.Open();
            da.Fill(DS);
            da.Dispose();
            con.Close();
            return DS;
        }

        public DataSet GetScope()
        {
            DataSet DS = new DataSet();
            System.Data.SqlClient.SqlConnection con = GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("dbo.GetScope", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            con.Open();
            da.Fill(DS);
            da.Dispose();
            con.Close();
            return DS;
        }

        public DataSet GetContext()
        {
            DataSet DS = new DataSet();
            System.Data.SqlClient.SqlConnection con = GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("dbo.GetContext", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            con.Open();
            da.Fill(DS);
            da.Dispose();
            con.Close();
            return DS;
        }

        public DataSet GetContextInScope(int i_ScopeID)
        {
            DataSet DS = new DataSet();
            System.Data.SqlClient.SqlConnection con = GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("dbo.GetContextInScope", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("int_ScopeID", i_ScopeID);
            con.Open();
            da.Fill(DS);
            da.Dispose();
            con.Close();
            return DS;
        }

        public DataSet GetBusinessEntitiesInContext(int i_ContextID)
        {
            DataSet DS = new DataSet();
            System.Data.SqlClient.SqlConnection con = GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("dbo.BusinessEntitiesInContext", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("Context_ID", i_ContextID);
            con.Open();
            da.Fill(DS);
            da.Dispose();
            con.Close();
            return DS;
        }

        public DataSet GetEntityTypes()
        {
            DataSet DS = new DataSet();
            System.Data.SqlClient.SqlConnection con = GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("dbo.GetEntityTypes", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            con.Open();
            da.Fill(DS);
            da.Dispose();
            con.Close();
            return DS;        
        }
        public DataSet GetEntityVersions()
        {
            DataSet DS = new DataSet();
            System.Data.SqlClient.SqlConnection con = GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("dbo.GetEntityVersions", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            con.Open();
            da.Fill(DS);
            da.Dispose();
            con.Close();
            return DS;                
        }

        public DataSet GetBusinessEntityInContextVersion(int i_BusinessEntityID)
        {
            DataSet DS = new DataSet();
            System.Data.SqlClient.SqlConnection con = GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("dbo.BusinessEntityInContextVersions", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("BusinessEntity_ID", i_BusinessEntityID);
            con.Open();
            da.Fill(DS);
            da.Dispose();
            con.Close();
            return DS;
        }

        public DataSet GetBusinessEntityDataDictionary(int i_ContextID, int i_BusinessEntityID)
        {
            DataSet DS = new DataSet();
            System.Data.SqlClient.SqlConnection con = GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("dbo.GetDataDictionaryForBusinessEntity", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("int_EntityTypeID", i_BusinessEntityID);
            da.SelectCommand.Parameters.AddWithValue("int_ContextID", i_ContextID);
            con.Open();
            da.Fill(DS);
            da.Dispose();
            con.Close();
            return DS;
        }

        public DataSet GetBusinessEntitiesInSolution(int i_SolutionID)
        {
            DataSet DS = new DataSet();
            System.Data.SqlClient.SqlConnection con = GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("dbo.GetBusinessEntitiesInSolution", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("Solution_ID", i_SolutionID);
            con.Open();
            da.Fill(DS);
            da.Dispose();
            con.Close();
            return DS;

        }

        /* Added Import functionality to import Entities to DB as tree is built  12/14/2009 */
        public DataSet ImportBusinessEntity(int i_ContextID, string sz_EntityTypeName)
        {
            DataSet DS = new DataSet();
            System.Data.SqlClient.SqlConnection con = GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("dbo.ImportBusinessEntity", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            /*
            con.Open();
            da.Fill(DS);
            da.Dispose();
            con.Close();
             */
            return DS;
        }

        public DataSet SaveContextualVersionEntity(string sz_ContextName,string sz_EntityTypeName,
            string sz_EntityMajorVersion, string sz_EntityMinorVersion, string sz_DataValueVersion,
            string sz_EntityCode, int IsBusinessEntity)
        {
            DataSet DS = new DataSet();
            System.Data.SqlClient.SqlConnection con = GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("dbo.SaveContextualVersionEntity", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("ContextName", sz_ContextName);
            da.SelectCommand.Parameters.AddWithValue("EntityTypeName", sz_EntityTypeName);
            da.SelectCommand.Parameters.AddWithValue("vch_EntityMajorVersion", sz_EntityMajorVersion);
            da.SelectCommand.Parameters.AddWithValue("vch_EntityMinorVersion", sz_EntityMinorVersion);
            da.SelectCommand.Parameters.AddWithValue("vch_DataValuesVersion", sz_DataValueVersion);
            da.SelectCommand.Parameters.AddWithValue("txt_EntityCode", sz_EntityCode);
            da.SelectCommand.Parameters.AddWithValue("b_IsBusinessEntity", IsBusinessEntity);
            con.Open();
            da.Fill(DS);
            da.Dispose();
            con.Close();
            return DS;
        }
        public DataSet GetEntityInstances(int i_EntityID)
        {
            DataSet DS = new DataSet();
            System.Data.SqlClient.SqlConnection con = GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("dbo.GetEntityInstances", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("int_EntityID", i_EntityID);
            con.Open();
            da.Fill(DS);
            da.Dispose();
            con.Close();
            return DS;        
        }
        public DataSet SaveEntityInstance(int i_EntityID, string xml_SerializedEntity, string vch_Description)
        {
            DataSet DS = new DataSet();
            System.Data.SqlClient.SqlConnection con = GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("dbo.SaveEntityInstance", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("int_EntityID", i_EntityID);
            da.SelectCommand.Parameters.AddWithValue("xml_SerializedEntity", xml_SerializedEntity);
            da.SelectCommand.Parameters.AddWithValue("vch_Description", vch_Description);
            con.Open();
            da.Fill(DS);
            da.Dispose();
            con.Close();
            return DS;        
        }
    }
}

